# -*- coding: utf-8 -*-

import os.path
import os

if 'DJANGO_SETTINGS_MODULE' not in os.environ:
    from django.core.management import setup_environ
    import wolfox.foxit.settings as settings
    setup_environ(settings)

from django.db import transaction
from wolfox.foxit.dune.models import Country,City,Exchange,StockType,CatalogSubject,Source,TransCalendar,Corporation,Catalog

@transaction.commit_on_success
def init_it():
    #国家
    china = Country.objects.create(code='CN',name=u'中国')
    america = Country.objects.create(code='US',name=u'美国')

    #城市
    shanghai = City.objects.create(code='SHA',name=u'上海',country = china)
    shenzhen = City.objects.create(code='SZX',name=u'深圳',country = china)
    xianggang = City.objects.create(code='HKG',name=u'香港',country = china)
    newyork = City.objects.create(code='NYC',name=u'纽约',country = america)

    #交易所
    #normal = Exchange(name=u'代码可区分交易场所',city=shanghai,code='CDSE',url='http://')   #该处为上海/深圳/香港等以前2位区分交易场所一类所指定的场地
    shse = Exchange.objects.create(code='SHSE',name=u'上海证券交易所',city=shanghai,abbr_code='SH',url='http://www.sse.com.cn')
    szse = Exchange.objects.create(code='SZSE',name=u'深圳证券交易所',city=shenzhen,abbr_code='SZ',url='http://www.szse.cn')
    hkex = Exchange.objects.create(code='HKEX',name=u'香港证券交易所',city=xianggang,code='HKEX',abbr_code='HK',url='http://www.hkex.com.hk')
    nyse = Exchange.objects.create(code='NYSE',name=u'纽约证券交易所',city=newyork,code='NYSE',abbr_code='NY',url='http://www.nyse.com')
    nasdaq = Exchange.objects.create(code='NASD',name=u'纳斯达克证券交易所',city=newyork,code='NASDAQ',abbr_code='NA',url='http://www.nasdaq.com')

    
    #证券类型
    stock = StockType.objects.create(code='STOCK',name=u'股票',remark='')
    fund = StockType.objects.create(code='FUND',name=u'基金',remark='')
    warrant = StockType.objects.create(code='WARR',name=u'权证',remark='')
    index = StockType.objects.create(code='INDEX',name=u'指数',remark='')
    sector = StockType.objects.create(code='SECT',name=u'板块',remark='') #板块
    out_fund = StockType.objects.create(code='OFUND',name=u'场外基金',remark='')

    #板块概念
    hy = CatalogSubject.objects.create(name=u'行业',code='HY',remark='')
    gn = CatalogSubject.objects.create(name=u'概念',code='GN',remark='')
    dy = CatalogSubject.objects.create(name=u'地域',code='DY',remark='')
    zhy = CatalogSubject.objects.create(name=u'证监会行业',code='ZHY',remark='')
    zsbk = CatalogSubject.objects.create(name=u'指数板块',code='ZS',remark='')
    xg = CatalogSubject.objects.create(name=u'香港板块',code='XG',remark='')
    qt = CatalogSubject.objects.create(name=u'其它板块',code='QT',remark='')


    #数据来源
    source = Source.objects.create(name=u'分析家')
    #分隔,使之后的corporation从1001开始
    corp = Corporation.objects.create(id=1000,name=u'unknown')
    
    #板块
    init_catalog()

    #日期初始化
    init_transcalendar(1990,2020)
    

def init_transcalendar(year_from,year_to):   #暂且为1990-2020
    import datetime
    from_date = datetime.date(year_from,1,1)
    to_date = datetime.date(year_to,1,1)
    step = datetime.timedelta(1)
    cur_date = from_date
    cur_year = cur_date.year - 1
    while cur_date < to_date:
        year,week,wday = cur_date.isocalendar()
        idate = year * 10000 + cur_date.month * 100 + cur_date.day
        wnum = year * 100 + week
        t = TransCalendar.objects.create(tdate=idate,month_num=year*100+cur_date.month,month_day=cur_date.day,week_num=wnum,week_day=wday)
        cur_date += step
        if cur_year != cur_date.year:
            print cur_year
            cur_year = cur_date.year



def init_catalog():
    block_def_file_name = os.path.join(os.path.dirname(__file__),'block.def')
    for line in open(block_def_file_name):
        if line[0] == '[':  #板块
            #cur_cs = CatalogSubject.objects.create(name=line[1:-2].decode('gbk'))
            cur_cs = CatalogSubject.objects.get(name=line[1:-2].decode('gbk'))
        else:   #板块内容
            print line[:-1]
            c = Catalog.objects.get_or_create(name=line[:-1].decode('gbk'),subject=cur_cs)


if __name__ == '__main__':  
    #init_catalog,初始化和更新板块类-名关系，需要保证block.def文件中原有的板块类-名关系不变化，只能新增
    init_catalog()
    
